const initEle = require('./initEle')()

module.exports = function (pageDefine, pageName, refs) {
  let _handles = {}

  let _pegeDef = pageDefine.default

  if (!_pegeDef.data) {
    _pegeDef.data = {}
  } else {
    _pegeDef.data = _pegeDef.data()
  }

  _pegeDef.data.__showed = 0

  if (!pageName && page.pageName) {
    pageName = page.pageName
  }
  for (let handleName in _pegeDef.handles) {
    _handles[handleName] = function(e) {
      _pegeDef.handles[handleName].apply(this, [e, e.currentTarget.dataset])
    }
  }

  if (!_pegeDef.onLoad) {
    _pegeDef.onLoad = function () {}
  }

  _pegeDef.onLoad = initEle.initStaticRef(_pegeDef.onLoad, refs, {
    from: 'page',
    fileName: pageName
  })

  return {
    onPullDownRefresh() {
      const app = getApp()
      let self = this
       if (app.listenPullDownRefresh) {
          app.listenPullDownRefresh(self).then(() => {
              if (self.awaitPullDownRefresh) {
                  self.awaitPullDownRefresh()
              }
          })
       }
    },
    ..._pegeDef,
    ..._pegeDef.methods,
    ..._handles,
  }
}
